package com.rongmei.jdsync.response;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Schema(description = "标准API响应格式")
public class ApiResponse<T> {
    @Schema(description = "是否成功", required = true, example = "true")
    private boolean success;

    @Schema(description = "响应码", required = true, example = "200")
    private int code;

    @Schema(description = "响应消息", required = true, example = "操作成功")
    private String message;

    @Schema(description = "响应数据")
    private T data;

    @Schema(description = "请求时间戳", example = "1672531200000")
    private long timestamp = System.currentTimeMillis();

    /**
     * 返回操作成功的ApiResponse对象
     *
     * @param <T>  泛型参数，表示ApiResponse中数据的类型
     * @param data 成功返回的数据
     * @return ApiResponse对象，包含状态、状态码、消息、数据和时间戳
     */
    public static <T> ApiResponse<T> success(T data) {
        return new ApiResponse<>(true, 200, "操作成功", data, System.currentTimeMillis());
    }


    /**
     * 创建一个表示错误的ApiResponse对象
     *
     * @param <T>     泛型类型，表示ApiResponse的泛型参数
     * @param code    错误码，表示错误类型
     * @param message 错误信息
     * @return 返回包含错误信息的ApiResponse对象
     */
    public static <T> ApiResponse<T> error(int code, String message) {
        return new ApiResponse<>(false, code, message, null, System.currentTimeMillis());
    }
}
